﻿using System;
using System.Data;
using UniMana.Data;
using UniMana.Utils.Data;
using UniMana.Utils.Configs;
using DevExpress.XtraEditors;

namespace UniMana.Win.Presonnel
{
    public partial class FrmPersonnelRegistSubject : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_data = new DBMain();
        private DataTable m_tblTable;
        private string m_strPersonID = "";
        private int m_iDepartmentKey;
        private Personnel_PersonnelInfo personinfo;

        public DevExpress.Utils.WaitDialogForm dlg;

        public FrmPersonnelRegistSubject()
        {
            InitializeComponent();
            gluPersonnel_PersonnelID.GetData(m_data.m_DCReadOnly.Personnel_Personnels, "ID", "ID");
            gluPersonnel_PersonnelID.Enabled = true;
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
            lbcTitle.Text = "Chọn giảng viên";
        }

        public FrmPersonnelRegistSubject(int m_iDepartmentKey, string m_strPersonID)
        {
            InitializeComponent();
            this.m_strPersonID = m_strPersonID;
            this.m_iDepartmentKey = m_iDepartmentKey;

            personinfo = new Personnel_PersonnelInfo(m_strPersonID);
            gluPersonnel_PersonnelID.EditValue = personinfo.Properties.ID;
            gluPersonnel_PersonnelID.Enabled = false;
            txtName.Text = personinfo.Properties.LastName + " " + personinfo.Properties.Name;
            LoadSubject();
            rluCatalogue_Department.GetData(m_data.m_DCReadOnly.Catalogue_Departments, "Catalogue_DepartmentKey", "Name");
            lbcTitle.Text = "Mã giảng viên";
        }

        private void FrmPersonnelRegistSubject_Load(object sender, EventArgs e)
        {
            if (!SysUtils.CheckAccessByRule(RuleConfig.MPT))
                return;
        }

        /// <summary>
        /// Lấy danh sách môn chưa đăng ký và đã đăng ký
        /// </summary>

        private void LoadSubject()
        {
            //try
            //{
                m_tblTable = (this.m_data.m_DCReadOnly.Training_Subjects.GetKeyIDNameTotalDepartmentPersonNotRegist(this.m_data.m_DCReadOnly.Personnel_Personnel_Subjects,
                    m_iDepartmentKey,
                    personinfo.Properties.Personnel_PersonnelKey.ToString())).ToDataTable();
                if (m_tblTable.Rows.Count > 0)
                {
                    // Sắp xếp lại dữ liệu
                    DataView dv = m_tblTable.DefaultView;
                    dv.Sort = "Catalogue_DepartmentKey ASC";
                    m_tblTable = dv.ToTable();
                    gctSubject.DataSource = m_tblTable;
                }
                else
                {
                    gctSubject.DataSource = null;
                }
                grcSubject.Text = "Tổng số môn hiện tại có " + m_tblTable.Rows.Count.ToString() + " môn";

                m_tblTable = (this.m_data.m_DCReadOnly.Training_Subjects.GetKeyIDNameTotalDepartmentPersonRegist(this.m_data.m_DCReadOnly.Personnel_Personnel_Subjects,
                    personinfo.Properties.Personnel_PersonnelKey.ToString())).ToDataTable();
                if (m_tblTable.Rows.Count > 0)
                {
                    // Sắp xếp lại dữ liệu
                    DataView dv = m_tblTable.DefaultView;
                    dv.Sort = "Catalogue_DepartmentKey ASC";
                    m_tblTable = dv.ToTable();
                    gctSubjectRegist.DataSource = m_tblTable;
                }
                else
                {
                    gctSubjectRegist.DataSource = null;
                }
                grcSubjectRegist.Text = "Số môn đã đăng ký " + m_tblTable.Rows.Count.ToString() + " môn";

                grvSubject.ExpandAllGroups();
                grvSubjectRegist.ExpandAllGroups();
            //}
            //catch { }
        }

        /// <summary>
        /// Xóa dữ liệu hiển thị
        /// </summary>
        
        private void ResetValues()
        {
            gctSubject.DataSource = null;
            gctSubjectRegist.DataSource = null;
        }

        /// <summary>
        /// Thêm môn cho giảng viên
        /// </summary>
        
        private void AddSubject()
        {
            try
            {
                if (grvSubject.SelectedRowsCount > 0)
                {
                    int[] index = grvSubject.GetSelectedRows();
                    for (int i = 0; i < index.Length; i++)
                    {
                        try
                        {
                            Personnel_Personnel_SubjectInfo subinfo = new Personnel_Personnel_SubjectInfo();
                            subinfo.Properties.Personnel_PersonnelKey = Guid.Parse(personinfo.Properties.Personnel_PersonnelKey.ToString());
                            subinfo.Properties.Training_SubjectKey = int.Parse(this.grvSubject.GetRowCellValue(index[i], "Training_SubjectKey").ToString());
                            subinfo.Properties.Status = false;
                            subinfo.Save();
                        }
                        catch { continue; }
                    }
                }
            }
            catch
            {
                XtraMessageBox.Show("Không có môn chọn");
            }
        }

        /// <summary>
        /// Xóa môn giảng viên đã đăng ký
        /// </summary>

        private void RemoveSubject()
        {
            try
            {
                if (grvSubjectRegist.SelectedRowsCount > 0)
                {
                    int[] index = grvSubjectRegist.GetSelectedRows();
                    for (int i = 0; i < index.Length; i++)
                    {
                        try
                        {
                            Personnel_Personnel_SubjectInfo subinfo = new Personnel_Personnel_SubjectInfo(personinfo.Properties.Personnel_PersonnelKey.ToString(),
                                int.Parse(this.grvSubjectRegist.GetRowCellValue(index[i], "Training_SubjectKey").ToString()));
                            subinfo.Delete();
                        }
                        catch { continue; }
                    }
                }
            }
            catch { XtraMessageBox.Show("Không có môn chọn"); }
        }

        /// <summary>
        /// Chọn thêm môn cho giảng viên
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private void btnAdd_Click(object sender, EventArgs e)
        {
            dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
            AddSubject();
            LoadSubject();
            dlg.Close();
        }
        
        /// <summary>
        /// Chọn xóa môn giảng viên đã đăng ký
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        
        private void btnRemove_Click(object sender, EventArgs e)
        {
            dlg = new DevExpress.Utils.WaitDialogForm(MsgConfig.MSG_WAIT, MsgConfig.MSG_LOADING);
            RemoveSubject();
            LoadSubject();
            dlg.Close();
        }

        private void gluPersonnel_PersonnelID_EditValueChanged(object sender, EventArgs e)
        {
            m_strPersonID = gluPersonnel_PersonnelID.EditValue.ToString();
            personinfo = new Personnel_PersonnelInfo(m_strPersonID);
            Personnel_PersonnelInfo pinfo = new Personnel_PersonnelInfo(m_strPersonID);
            if (pinfo.Properties.ID != "")
            {
                m_iDepartmentKey = int.Parse(pinfo.Properties.Catalogue_DepartmentKey.ToString());
                txtName.Text = pinfo.Properties.LastName + " " + pinfo.Properties.Name;
                LoadSubject();
            }
        }
    }
}
